<template>
  <div class="index">
    <el-container>
      <el-header height="68px">
        <m-header></m-header>
      </el-header>
      <el-container>
        <el-aside width="200px">
          <m-aside></m-aside>
        </el-aside>
        <el-main>
          <div class="main">
            <div class="header-top">
              <el-breadcrumb separator-class="el-icon-arrow-right">
                <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
                <el-breadcrumb-item>用户管理</el-breadcrumb-item>
              </el-breadcrumb>
              <h2 class="title">用户管理</h2>
            </div>
            <div class="row">
              <el-form :inline="true" :model="formInline"  class="demo-form-inline">
                <div class="water">

                  <span class="waterTime">注册时间：</span>
                  <el-date-picker
                    v-model="formInline.time"
                    type="daterange"
                    range-separator="至"
                    start-placeholder="开始日期"
                    end-placeholder="结束日期">
                  </el-date-picker>
                </div>
                <el-form-item label="手机：">
                  <el-input v-model="formInline.iphone" placeholder="手机号"></el-input>
                </el-form-item>
     
                <el-form-item label="真实姓名：">
                  <el-input v-model="formInline.name" placeholder="真实姓名"></el-input>
                </el-form-item>
                
                <el-form-item>
                  <el-button type="primary" @click="onSubmit">搜索</el-button>
                </el-form-item>
              </el-form>

              <el-table
                :data="tableData"
                border
                :cell-style="{'text-align':'center'}"

                :header-cell-style="{
                  'background-color': '#f5f5f5',
                  'color':'#555',
                  'text-align':'center'
                }"
                style="width: 100%">
                
                <el-table-column
                  prop="phoneNumber"
                  label="手机">
                </el-table-column>  
                <el-table-column
                  prop="publisherName"
                  label="真实姓名">
                </el-table-column>
                <el-table-column
                  prop="isPublisher"
                  label="是否商家">
                  <template slot-scope="scope">
                    <el-button @click="PublisherClick(scope.row)" type="text" size="small" v-if="scope.row.isPublisher == '0'">普通用户</el-button>
                    <el-button @click="PublisherClick(scope.row)" type="text" size="small" v-if="scope.row.isPublisher == '1'">商户</el-button>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="isActive"
                  label="状态">
                  <template slot-scope="scope">
                    <el-button @click="statusClick(scope.row)" type="text" size="small" v-if="scope.row.isActive == '0'">不可用</el-button>
                    <el-button @click="statusClick(scope.row)" type="text" size="small" v-if="scope.row.isActive == '1'">可用</el-button>
                  </template>
                </el-table-column>
                <el-table-column
                  prop="createDate"
                  label="注册时间">
                </el-table-column>
                
                <el-table-column
                  label="操作"
                  width="360">
                  <template slot-scope="scope">
                    <el-button @click="redactClick(scope.row);dialogVisible = true" type="text" size="small">编辑</el-button>
                  </template>
                </el-table-column>
                
              </el-table>
              
              <div class="block">
                <el-pagination
                  @size-change="handleSizeChange"
                  @current-change="handleCurrentChange"
                  :current-page="currentPage"
                  :page-sizes="[20, 50, 100]"
                  :page-size="pagesize"
                  layout="total, sizes, prev, pager, next, jumper"
                  :total="total">
                </el-pagination>
              </div>
    
            </div>

          </div>
        </el-main>
      </el-container>
      <el-dialog
        :visible.sync="dialogVisible"

        :close-on-click-modal='boole'
        custom-class="diaself"
        center
        top='30vh'
        width="28%">
        <el-form  :model="form" label-position="right" label-width="70px" class="text-style">
          <el-form-item label="手机号">
            <el-input v-model="form.phoneNumber" :disabled="true"></el-input>
          </el-form-item>
          <el-form-item label="注册时间">

            <span >{{form.createDate}}</span>

          </el-form-item>
          <el-form-item label="账户类型" prop="isPublisher" >
            <el-radio-group v-model="form.isPublisher">
              <el-radio v-model="form.isPublisher" label="0" >普通用户</el-radio>
              <el-radio v-model="form.isPublisher" label="1" >商户</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="状态" prop="isActive" >
            <el-radio-group v-model="form.isActive">
              <el-radio v-model="form.isActive" label="1" >可用</el-radio>
              <el-radio v-model="form.isActive" label="0" >不可用</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item >
            <el-button type="primary" @click="submit" size="medium">提 交</el-button>
          </el-form-item>
          
        </el-form>
        
      </el-dialog>
    </el-container>
  </div>
</template>
<script>
import { getCookie,timeChange,getQueryString } from "./until/index";
function init(){
  this.$ajax.get("admGetUserList.ashx?token=" +getCookie("token")+'&phoneNumber='+this.formInline.iphone+
        '&dateFrom='+this.startTime +
        '&dateTo='+this.endTime +
        '&publisherName='+this.formInline.name+
        '&pz='+this.pagesize+
        '&pi='+this.currentPage
      )
      .then(res => {
        console.log(res)
        if (res.data.code === "200") {
          var data = res.data.data;
          
          this.total = Number(data.rowCount)
          if(data.rowCount > 0){

            this.tableData = data.userList;
          }else{
            this.tableData=[]
          }
          
        } else {
          this.$message.error(res.data.msg );
        }
      })
      .catch(error => {
        console.log(error);
      });
}
function userStatus(userID){
  this.$ajax.get("admChangeUserStatus.ashx?token=" +getCookie("token")+
        '&userID='+userID
      )
      .then(res => {
        if (res.data.code === "200") {
          var data = res.data.data;
        
         this.$message({
            type: 'success',
            message: res.data.msg,
            onClose:function(){

              window.location.reload();
            }
          }); 
        } else {
          this.$message.error(res.data.msg );
        }
      })
      .catch(error => {
        console.log(error);
      });
}
function changeUserRole(userID){
  this.$ajax.get("admChangeUserRole.ashx?token=" +getCookie("token")+
        '&userID='+userID
      )
      .then(res => {
        if (res.data.code === "200") {
          var data = res.data.data;
        
         this.$message({
            type: 'success',
            message: res.data.msg,
            onClose:function(){

              window.location.reload();
            }
          }); 
        } else {
          this.$message.error(res.data.msg );
        }
      })
      .catch(error => {
        console.log(error);
      });
}
export default {
  data() {
    return {
      pagesize: 20, //每页显示条数
      currentPage: 1, //初始页
      total: 0, //总条数
      startTime:'',
      endTime:'',
      dialogVisible:false,
      boole:false,
      form:{
        userID:'',
        phoneNumber:'',
        createDate:'',
        isPublisher:'',
        isActive:''
      },
      formInline: {
        time:'',//查询时间
        name: '', //用户昵称/ID
        iphone:'',  //用户手机号
      },
      tableData: [],

    };
  },
  created() {
    
    if(getQueryString()){
      
      this.formInline.name = getQueryString()
    }
    init.call(this)
  },
  methods: {
    submit(){

    },
    onSubmit() {
      if(this.formInline.time){

        this.startTime = timeChange(this.formInline.time[0])
        this.endTime = timeChange(this.formInline.time[1])
      }
      
      
      init.call(this)
    },
    handleCurrentChange(num) {
      this.currentPage = num;
      init.call(this)
    },
    handleSizeChange(val) {
      this.pagesize = val;
      init.call(this)
    },
    
    statusClick(row){//改变用户状态
      
        
      this.$confirm('是否要改变此用户状态？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        userStatus.call(this,row.userID)
        
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消退出'
        });          
      });
      
    
    },
    redactClick(row){//编辑
      
      this.form.userID = row.userID
      this.form.phoneNumber = row.phoneNumber
      this.form.isActive = String(row.isActive)
      this.form.isPublisher = String(row.isPublisher)
      this.form.createDate = row.createDate
      console.log(this.form)
      
      
    },
    PublisherClick(row){//修改商户
        this.$confirm('是否要改变此用户角色？', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          changeUserRole.call(this,row.userID)
          
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消退出'
          });          
        });
    }



    
  }
};
</script>
<style scoped>
.index {
  overflow: hidden;
}
.water{margin-right: 20px;display: inline-block}
.waterTime{font-size: 14px;color: #606266;padding: 0 12px 0 0;box-sizing: border-box;}
.el-header {
  background-color: #3a4759;
  color: #fff;
}
.el-aside {
  background-color: #485668;
  color: #333;
  text-align: center;
  line-height: 200px;
  overflow: hidden;
}
.el-main {
  background-color: #f0f2f5;
  color: #333;
  text-align: center;
  height: 100%;
  min-height: 740px
}

.header-top{
  margin: -20px -20px 20px;
  padding: 15px 20px;
  background: #fff;
  
}
.header-top .title{
  margin:15px 0 0;
  font-weight: 600;
  text-align: center;
  font-size: 20px;
}
.row{
  background: #fff;
  padding: 20px 30px;
  text-align: left
}
.block {
  padding: 20px;
}
.message{
  margin-top: 20px;
}
</style>
  